home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / src-16f.lha / code / ntrace.lisp < prev    next >
Encoding:
Text File  |  1992-12-09  |  22.6 KB  |  675 lines

  1. ;;; -*- Package: debug -*-
  2. ;;;
  3. ;;; **********************************************************************
  4. ;;; This code was written as part of the CMU Common Lisp project at
  5. ;;; Carnegie Mellon University, and has been placed in the public domain.
  6. ;;; If you want to use this code or any part of CMU Common Lisp, please contact
  7. ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
  8. ;;;
  9. (ext:file-comment
  10.   "$Header: ntrace.lisp,v 1.8.1.1 92/12/08 23:29:07 ram Exp $")
  11. ;;;
  12. ;;; **********************************************************************
  13. ;;;
  14. ;;; This is a tracing facility based on breakpoints.
  15. ;;;
  16. ;;; Written by Rob MacLachlan and Bill Chiles.
  17. ;;;
  18. ;;; **********************************************************************
  19. ;;;
  20. (in-package "LISP")
  21.  
  22. (export '(trace untrace))
  23.  
  24. (in-package "DEBUG")
  25.  
  26. (export '(*trace-values* *max-trace-indentation* *trace-encapsulate-default*))
  27.  
  28. (defvar *trace-values* nil
  29.   "This is bound to the returned values when evaluating :BREAK-AFTER and
  30.    :PRINT-AFTER forms.")
  31.  
  32. (defvar *max-trace-indentation* 40
  33.   "If the trace indentation exceeds this value, then indentation restarts at
  34.    0.")
  35.  
  36. (defvar *trace-encapsulate-default* :default
  37.   "The default value for the :ENCAPSULATE option to trace.")
  38.  
  39.  
  40. ;;;; Internal state:
  41.  
  42. ;;; A hash-table that maps each traced function to the TRACE-INFO.  The entry
  43. ;;; for a closure is the shared function-entry object.
  44. ;;;
  45. (defvar *traced-functions* (make-hash-table :test #'eq))
  46.  
  47. ;;; The TRACE-INFO structure represents all the information we need to trace a
  48. ;;; given function.
  49. ;;;
  50. (defstruct (trace-info
  51.         (:print-function
  52.          (lambda (s stream d)
  53.            (declare (ignore d))
  54.            (print-unreadable-object (s stream)
  55.          (format stream "Trace-Info ~S" (trace-info-what s)))))
  56.         (:make-load-form-fun :just-dump-it-normally))
  57.   ;;
  58.   ;; The original representation of the thing traced.
  59.   (what nil :type (or function cons symbol))
  60.   ;;
  61.   ;; True if What is a function name whose definition we should track.
  62.   (named nil)
  63.   ;;
  64.   ;; True if tracing is to be done by encapsulation rather than breakpoints.
  65.   ;; T implies Named.
  66.   (encapsulated *trace-encapsulate-default*)
  67.   ;;
  68.   ;; True if this trace has been untraced.
  69.   (untraced nil)
  70.   ;;
  71.   ;; Breakpoints we set up to trigger tracing.
  72.   (start-breakpoint nil :type (or di:breakpoint null))
  73.   (end-breakpoint nil :type (or di:breakpoint null))
  74.   ;;
  75.   ;; The list of function names for wherein.  NIL means unspecified.
  76.   (wherein nil :type list)
  77.   ;;
  78.   ;; The following slots represent the forms that we are supposed to evaluate
  79.   ;; on each iteration.  Each form is represented by a cons (Form . Function),
  80.   ;; where the Function is the cached result of coercing Form to a function.
  81.   ;; Forms which use the current environment are converted with
  82.   ;; PREPROCESS-FOR-EVAL, which gives us a one-arg function.  
  83.   ;; Null environment forms also have one-arg functions, but the argument is
  84.   ;; ignored.  NIL means unspecified (the default.)
  85.   ;;
  86.   ;; Current environment forms:
  87.   (condition nil)
  88.   (break nil)
  89.   ;;
  90.   ;; List of current environment forms:
  91.   (print () :type list)
  92.   ;;
  93.   ;; Null environment forms.
  94.   (condition-after nil)
  95.   (break-after nil)
  96.   ;;
  97.   ;; List of null environment forms
  98.   (print-after () :type list))
  99.  
  100. ;;; This is a list of conses (function-end-cookie . condition-satisfied),
  101. ;;; which we use to note distinct dynamic entries into functions.  When we
  102. ;;; enter a traced function, we add a entry to this list holding the new
  103. ;;; end-cookie and whether the trace condition was statisfied.  We must save
  104. ;;; the trace condition so that the after breakpoint knows whether to print.
  105. ;;; The length of this list tells us the indentation to use for printing TRACE
  106. ;;; messages.
  107. ;;;
  108. ;;; This list also helps us synchronize the TRACE facility dynamically for
  109. ;;; detecting non-local flow of control.  Whenever execution hits a
  110. ;;; :function-end breakpoint used for TRACE'ing, we look for the
  111. ;;; function-end-cookie at the top of *traced-entries*.  If it is not there, we
  112. ;;; discard any entries that come before our cookie.
  113. ;;;
  114. ;;; When we trace using encapsulation, we bind this variable and add
  115. ;;; (nil . condition-satisfied), so a NIL "cookie" marks an encapsulated
  116. ;;; tracing.
  117. ;;;
  118. (defvar *traced-entries* ())
  119. (declaim (list *traced-entries*))
  120.  
  121. ;;; This variable is used to discourage infinite recursions when some trace
  122. ;;; action invokes a function that is itself traced.  In this case, we quietly
  123. ;;; ignore the inner tracing.
  124. ;;;
  125. (defvar *in-trace* nil)
  126.  
  127.  
  128. ;;;; Utilities:
  129.  
  130. ;;; TRACE-FDEFINITION  --  Internal
  131. ;;;
  132. ;;;    Given a function name, a function or a macro name, return the raw
  133. ;;; definition and some information.  "Raw"  means that if the result is a
  134. ;;; closure, we strip off the closure and return the bare code.  The second
  135. ;;; value is T if the argument was a function name.  The third value is one of
  136. ;;; :COMPILED, :COMPILED-CLOSURE, :INTERPRETED, :INTERPRETED-CLOSURE and
  137. ;;; :FUNCALLABLE-INSTANCE.
  138. ;;;
  139. (defun trace-fdefinition (x)
  140.   (multiple-value-bind
  141.       (res named-p)
  142.       (typecase x
  143.     (symbol
  144.      (cond ((special-form-p x)
  145.         (error "Can't trace special form ~S." x))
  146.            ((macro-function x))
  147.            (t
  148.         (values (fdefinition x) t))))
  149.     (function x)
  150.     (t (values (fdefinition x) t)))
  151.     (if (eval:interpreted-function-p res)
  152.     (values res named-p (if (eval:interpreted-function-closure res)
  153.                 :interpreted-closure :interpreted))
  154.     (case (kernel:get-type res)
  155.       (#.vm:closure-header-type
  156.        (values (kernel:%closure-function res) named-p :compiled-closure))
  157.       (#.vm:funcallable-instance-header-type
  158.        (values res named-p :funcallable-instance))
  159.       (t (values res named-p :compiled))))))
  160.  
  161.  
  162. ;;; TRACE-REDEFINED-UPDATE  --  Internal
  163. ;;;
  164. ;;;    When a function name is redefined, and we were tracing that name, then
  165. ;;; untrace the old definition and trace the new one.
  166. ;;;
  167. (defun trace-redefined-update (fname new-value)
  168.   (when (fboundp fname)
  169.     (let* ((fun (trace-fdefinition fname))
  170.        (info (gethash fun *traced-functions*)))
  171.       (when (and info (trace-info-named info))
  172.     (untrace-1 fname)
  173.     (trace-1 fname info new-value)))))
  174. ;;;
  175. (push #'trace-redefined-update ext:*setf-fdefinition-hook*)
  176.  
  177.  
  178. ;;; COERCE-FORM, COERCE-FORM-LIST  --  Internal
  179. ;;;
  180. ;;;    Annotate some forms to evaluate with pre-converted functions.  Each form
  181. ;;; is really a cons (exp . function).  Loc is the code location to use for
  182. ;;; the lexical environment.  If Loc is NIL, evaluate in the null environment.
  183. ;;; If Form is NIL, just return NIL.
  184. ;;;
  185. (defun coerce-form (form loc)
  186.   (when form
  187.     (let ((exp (car form)))
  188.       (if (di:code-location-p loc)
  189.       (let ((fun (di:preprocess-for-eval exp loc)))
  190.         (cons exp
  191.           #'(lambda (frame)
  192.               (let ((*current-frame* frame))
  193.             (funcall fun frame)))))
  194.       (let* ((bod (ecase loc
  195.             ((nil) exp)
  196.             (:encapsulated
  197.              `(flet ((debug:arg (n)
  198.                    (declare (special argument-list))
  199.                    (elt argument-list n)))
  200.                 (declare (ignorable #'debug:arg))
  201.                 ,exp))))
  202.          (fun (coerce `(lambda () ,bod) 'function)))
  203.         (cons exp
  204.           #'(lambda (frame)
  205.               (declare (ignore frame))
  206.               (let ((*current-frame* nil))
  207.             (funcall fun)))))))))
  208. ;;;
  209. (defun coerce-form-list (forms loc)
  210.   (mapcar #'(lambda (x) (coerce-form x loc)) forms))
  211.  
  212.           
  213. ;;; PRINT-TRACE-INDENTATION  --  Internal
  214. ;;;
  215. ;;;    Print indentation according to the number of trace entries.  Entries
  216. ;;; whose condition was false don't count.
  217. ;;;
  218. (defun print-trace-indentation ()
  219.   (let ((depth 0))
  220.     (dolist (entry *traced-entries*)
  221.       (when (cdr entry) (incf depth)))
  222.     (format t "~@V,0T~D: "
  223.         (+ (mod (* depth 2) (- *max-trace-indentation* 2)) 2)
  224.         depth)))
  225.  
  226.  
  227. ;;; TRACE-WHEREIN-P -- Internal.
  228. ;;;
  229. ;;;    Return true if one of the Names appears on the stack below Frame.
  230. ;;;
  231. (defun trace-wherein-p (frame names)
  232.   (do ((frame (di:frame-down frame) (di:frame-down frame)))
  233.       ((not frame) nil)
  234.     (when (member (di:debug-function-name (di:frame-debug-function frame))
  235.           names :test #'equal)
  236.       (return t))))
  237.  
  238. ;;; TRACE-PRINT  --  Internal
  239. ;;;
  240. ;;;    Handle print and print-after options.
  241. ;;;
  242. (defun trace-print (frame forms)
  243.   (dolist (ele forms)
  244.     (fresh-line)
  245.     (print-trace-indentation)
  246.     (format t "~S = ~S" (car ele) (funcall (cdr ele) frame))))
  247.  
  248. ;;; TRACE-MAYBE-BREAK  --  Internal
  249. ;;;
  250. ;;;    Test a break option, and break if true.
  251. ;;;
  252. (defun trace-maybe-break (info break where frame)
  253.   (when (and break (funcall (cdr break) frame))
  254.     (di:flush-frames-above frame)
  255.     (let ((*stack-top-hint* frame))
  256.       (break "Breaking ~A traced call to ~S:" where
  257.          (trace-info-what info)))))
  258.  
  259. ;;; DISCARD-INVALID-ENTRIES  --  Internal
  260. ;;;
  261. ;;;    This function discards any invalid cookies on our simulated stack.
  262. ;;; Encapsulated entries are always valid, since we bind *traced-entries* in
  263. ;;; the encapsulation.
  264. ;;;
  265. (defun discard-invalid-entries (frame)
  266.   (loop
  267.     (when (or (null *traced-entries*)
  268.           (let ((cookie (caar *traced-entries*)))
  269.         (or (not cookie)
  270.             (di:function-end-cookie-valid-p frame cookie))))
  271.       (return))
  272.     (pop *traced-entries*)))
  273.  
  274.  
  275. ;;;; Hook functions:
  276.  
  277. ;;; TRACE-START-BREAKPOINT-FUN -- Internal.
  278. ;;;
  279. ;;;    Return a closure that can be used for a function start breakpoint hook
  280. ;;; function and a closure that can be used as the FUNCTION-END-COOKIE
  281. ;;; function.  The first communicates the sense of the Condition to the second
  282. ;;; via a closure variable.
  283. ;;;
  284. (defun trace-start-breakpoint-fun (info)
  285.   (let (conditionp)
  286.     (values
  287.      #'(lambda (frame bpt)
  288.      (declare (ignore bpt))
  289.      (discard-invalid-entries frame)
  290.      (let ((condition (trace-info-condition info))
  291.            (wherein (trace-info-wherein info)))
  292.        (setq conditionp
  293.          (and (not *in-trace*)
  294.               (or (not condition)
  295.               (funcall (cdr condition) frame))
  296.               (or (not wherein)
  297.               (trace-wherein-p frame wherein)))))
  298.      
  299.      (when conditionp
  300.        (let ((*print-length* (or *debug-print-length* *print-length*))
  301.          (*print-level* (or *debug-print-level* *print-level*))
  302.          (kernel:*current-level* 0)
  303.          (*standard-output* *trace-output*)
  304.          (*in-trace* t))
  305.          (fresh-line)
  306.          (print-trace-indentation)
  307.          (if (trace-info-encapsulated info)
  308.          (locally (declare (special basic-definition argument-list))
  309.            (prin1 `(,(trace-info-what info) ,@argument-list)))
  310.          (print-frame-call-1 frame nil))
  311.          (terpri)
  312.          (trace-print frame (trace-info-print info)))
  313.        (trace-maybe-break info (trace-info-break info) "before" frame)))
  314.  
  315.      #'(lambda (frame cookie)
  316.      (declare (ignore frame))
  317.      (push (cons cookie conditionp) *traced-entries*)))))
  318.  
  319.  
  320. ;;; TRACE-END-BREAKPOINT-FUN  --  Internal
  321. ;;; 
  322. ;;;    This prints a representation of the return values delivered.  First,
  323. ;;; this checks to see that cookie is at the top of *traced-entries*; if it is
  324. ;;; not, then we need to adjust this list to determine the correct indentation
  325. ;;; for output.  We then check to see if the function is still traced and that
  326. ;;; the condition succeeded before printing anything.
  327. ;;;
  328. (defun trace-end-breakpoint-fun (info)
  329.   #'(lambda (frame bpt *trace-values* cookie)
  330.       (declare (ignore bpt))
  331.       (unless (eq cookie (caar *traced-entries*))
  332.     (setf *traced-entries*
  333.           (member cookie *traced-entries* :key #'car)))
  334.       
  335.       (let ((entry (pop *traced-entries*)))
  336.     (when (and (not (trace-info-untraced info))
  337.            (or (cdr entry)
  338.                (let ((cond (trace-info-condition-after info)))
  339.              (and cond (funcall (cdr cond) frame)))))
  340.       (let ((*print-length* (or *debug-print-length* *print-length*))
  341.         (*print-level* (or *debug-print-level* *print-level*))
  342.         (kernel:*current-level* 0)
  343.         (*standard-output* *trace-output*)
  344.         (*in-trace* t))
  345.         (fresh-line)
  346.         (pprint-logical-block (*standard-output* nil)
  347.           (print-trace-indentation)
  348.           (pprint-indent :current 2)
  349.           (format t "~S returned" (trace-info-what info))
  350.           (dolist (v *trace-values*)
  351.         (write-char #\space)
  352.         (pprint-newline :linear)
  353.         (prin1 v)))
  354.         (terpri)
  355.         (trace-print frame (trace-info-print-after info)))
  356.       (trace-maybe-break info (trace-info-break-after info)
  357.                  "after" frame)))))
  358.  
  359.  
  360. ;;; TRACE-CALL  --  Internal
  361. ;;;
  362. ;;;    This function is called by the trace encapsulation.  It calls the
  363. ;;; breakpoint hook functions with NIL for the breakpoint and cookie, which
  364. ;;; we have cleverly contrived to work for our hook functions.
  365. ;;;
  366. (defun trace-call (info)
  367.   (multiple-value-bind
  368.       (start cookie)
  369.       (trace-start-breakpoint-fun info)
  370.     (let ((frame (di:frame-down (di:top-frame))))
  371.       (funcall start frame nil)
  372.       (let ((*traced-entries* *traced-entries*))
  373.     (declare (special basic-definition argument-list))
  374.     (funcall cookie frame nil)
  375.     (let ((vals
  376.            (multiple-value-list
  377.         (apply basic-definition argument-list))))
  378.       (funcall (trace-end-breakpoint-fun info) frame nil vals nil)
  379.       (values-list vals))))))
  380.  
  381.  
  382. ;;; TRACE-1 -- Internal.
  383. ;;;
  384. ;;;    Trace one function according to the specified options.  We copy the
  385. ;;; trace info (it was a quoted constant), fill in the functions, and then
  386. ;;; install the breakpoints or encapsulation.
  387. ;;;
  388. ;;;    If non-null, Definition is the new definition of a function that we are
  389. ;;; automatically retracing; this 
  390. ;;;
  391. (defun trace-1 (function-or-name info &optional definition)
  392.   (multiple-value-bind
  393.       (fun named kind)
  394.       (if definition
  395.       (values definition t
  396.           (nth-value 2 (trace-fdefinition definition)))
  397.       (trace-fdefinition function-or-name))
  398.     (when (gethash fun *traced-functions*)
  399.       (warn "Function ~S already TRACE'd, retracing it." function-or-name)
  400.       (untrace-1 fun))
  401.     
  402.     (let* ((debug-fun (di:function-debug-function fun))
  403.        (encapsulated 
  404.         (if (eq (trace-info-encapsulated info) :default)
  405.         (ecase kind
  406.           (:compiled nil)
  407.           (:compiled-closure
  408.            (unless (functionp function-or-name)
  409.              (warn "Tracing shared code for ~S:~%  ~S"
  410.                function-or-name fun))
  411.            nil)
  412.           ((:interpreted :interpreted-closure :funcallable-instance)
  413.            t))
  414.         (trace-info-encapsulated info)))
  415.        (loc (if encapsulated
  416.             :encapsulated
  417.             (di:debug-function-start-location debug-fun)))
  418.        (info (make-trace-info
  419.           :what function-or-name
  420.           :named named
  421.           :encapsulated encapsulated
  422.           :wherein (trace-info-wherein info)
  423.           :condition (coerce-form (trace-info-condition info) loc)
  424.           :break (coerce-form (trace-info-break info) loc)
  425.           :print (coerce-form-list (trace-info-print info) loc)
  426.           :break-after (coerce-form (trace-info-break-after info) nil)
  427.           :condition-after
  428.           (coerce-form (trace-info-condition-after info) nil)
  429.           :print-after
  430.           (coerce-form-list (trace-info-print-after info) nil))))
  431.  
  432.       (dolist (wherein (trace-info-wherein info))
  433.     (unless (or (stringp wherein)
  434.             (fboundp wherein))
  435.       (warn ":WHEREIN name is not a defined global function: ~S"
  436.         wherein)))
  437.  
  438.       (cond
  439.        (encapsulated
  440.     (unless named
  441.       (error "Can't use encapsulation to trace anonymous function ~S."
  442.          fun))
  443.     (ext:encapsulate function-or-name 'trace `(trace-call ',info)))
  444.        (t
  445.     (multiple-value-bind
  446.         (start-fun cookie-fun)
  447.         (trace-start-breakpoint-fun info)
  448.       (let ((start (di:make-breakpoint start-fun debug-fun
  449.                        :kind :function-start))
  450.         (end (di:make-breakpoint
  451.               (trace-end-breakpoint-fun info)
  452.               debug-fun :kind :function-end
  453.               :function-end-cookie cookie-fun)))
  454.         (setf (trace-info-start-breakpoint info) start)
  455.         (setf (trace-info-end-breakpoint info) end)
  456.         ;;
  457.         ;; The next two forms must be in the order in which they appear,
  458.         ;; since the start breakpoint must run before the function-end
  459.         ;; breakpoint's start helper (which calls the cookie function.)
  460.         ;; One reason is that cookie function requires that the CONDITIONP
  461.         ;; shared closure variable be initialized.
  462.         (di:activate-breakpoint start)
  463.         (di:activate-breakpoint end)))))
  464.  
  465.       (setf (gethash fun *traced-functions*) info)))
  466.  
  467.   function-or-name)
  468.  
  469.  
  470. ;;;; The TRACE macro:
  471.  
  472. ;;;  PARSE-TRACE-OPTIONS  --  Internal
  473. ;;;
  474. ;;;    Parse leading trace options off of Specs, modifying Info accordingly.
  475. ;;; The remaining portion of the list is returned when we encounter a plausible
  476. ;;; function name.
  477. ;;;
  478. (defun parse-trace-options (specs info)
  479.   (let ((current specs))
  480.     (loop
  481.       (when (endp current) (return))
  482.       (let ((option (first current))
  483.         (value (cons (second current) nil)))
  484.     (case option
  485.       (:condition (setf (trace-info-condition info) value))
  486.       (:condition-after
  487.        (setf (trace-info-condition info) (cons nil nil))
  488.        (setf (trace-info-condition-after info) value))
  489.       (:condition-all
  490.        (setf (trace-info-condition info) value)
  491.        (setf (trace-info-condition-after info) value))
  492.       (:wherein
  493.        (setf (trace-info-wherein info)
  494.          (if (listp (car value)) (car value) value)))
  495.       (:encapsulate
  496.        (setf (trace-info-encapsulated info) (car value)))
  497.       (:break (setf (trace-info-break info) value))
  498.       (:break-after (setf (trace-info-break-after info) value))
  499.       (:break-all
  500.        (setf (trace-info-break info) value)
  501.        (setf (trace-info-break-after info) value))
  502.       (:print
  503.        (setf (trace-info-print info)
  504.          (append (trace-info-print info) (list value))))
  505.       (:print-after
  506.        (setf (trace-info-print-after info)
  507.          (append (trace-info-print-after info) (list value))))
  508.       (:print-all
  509.        (setf (trace-info-print info)
  510.          (append (trace-info-print info) (list value)))
  511.        (setf (trace-info-print-after info)
  512.          (append (trace-info-print-after info) (list value))))
  513.       (t (return)))
  514.     (pop current)
  515.     (unless current
  516.       (error "Missing argument to ~S TRACE option." option))
  517.     (pop current)))
  518.     current))
  519.  
  520.  
  521. ;;; EXPAND-TRACE  --  Internal
  522. ;;;
  523. ;;;    Compute the expansion of TRACE in the non-trivial case (arguments
  524. ;;; specified.)  If there are no :FUNCTION specs, then don't use a LET.  This
  525. ;;; allows TRACE to be used without the full interpreter.
  526. ;;;
  527. (defun expand-trace (specs)
  528.   (collect ((binds)
  529.         (forms))
  530.     (let* ((global-options (make-trace-info))
  531.        (current (parse-trace-options specs global-options)))
  532.       (loop
  533.     (when (endp current) (return))
  534.     (let ((name (pop current))
  535.           (options (copy-trace-info global-options)))
  536.       (cond
  537.        ((eq name :function)
  538.         (let ((temp (gensym)))
  539.           (binds `(,temp ,(pop current)))
  540.           (forms `(trace-1 ,temp ',options))))
  541.        ((and (keywordp name)
  542.          (not (or (fboundp name) (macro-function name))))
  543.         (error "Unknown TRACE option: ~S" name))
  544.        (t
  545.         (forms `(trace-1 ',name ',options))))
  546.       (setq current (parse-trace-options current options)))))
  547.  
  548.     (if (binds)
  549.     `(let ,(binds) (list ,@(forms)))
  550.     `(list ,@(forms)))))
  551.  
  552.  
  553. ;;; %LIST-TRACED-FUNCTIONS  --  Internal
  554. ;;;
  555. (defun %list-traced-functions ()
  556.   (loop for x being each hash-value in *traced-functions*
  557.         collect (trace-info-what x)))
  558.  
  559.  
  560. ;;; TRACE -- Public.
  561. ;;;
  562. (defmacro trace (&rest specs)
  563.   "TRACE {Option Global-Value}* {Name {Option Value}*}*
  564.    TRACE is a debugging tool that prints information when specified functions
  565.    are called.  In its simplest form:
  566.        (trace Name-1 Name-2 ...)
  567.  
  568.    TRACE causes a printout on *TRACE-OUTPUT* each time that one of the named
  569.    functions is entered or returns (the Names are not evaluated.)  The output
  570.    is indented according to the number of pending traced calls, and this trace
  571.    depth is printed at the beginning of each line of output.
  572.  
  573.    Options allow modification of the default behavior.  Each option is a pair
  574.    of an option keyword and a value form.  Options may be interspersed with
  575.    function names.  Options only affect tracing of the function whose name they
  576.    appear immediately after.  Global options are specified before the first
  577.    name, and affect all functions traced by a given use of TRACE.
  578.  
  579.    The following options are defined:
  580.  
  581.    :CONDITION Form
  582.    :CONDITION-AFTER Form
  583.    :CONDITION-ALL Form
  584.        If :CONDITION is specified, then TRACE does nothing unless Form
  585.        evaluates to true at the time of the call.  :CONDITION-AFTER is
  586.        similar, but suppresses the initial printout, and is tested when the
  587.        function returns.  :CONDITION-ALL tries both before and after.
  588.  
  589.    :WHEREIN Names
  590.        If specified, Names is a function name or list of names.  TRACE does
  591.        nothing unless a call to one of those functions encloses the call to
  592.        this function (i.e. it would appear in a backtrace.)  Anonymous
  593.        functions have string names like \"DEFUN FOO\".
  594.  
  595.    :BREAK Form
  596.    :BREAK-AFTER Form
  597.    :BREAK-ALL Form
  598.        If specified, and Form evaluates to true, then the debugger is invoked
  599.        at the start of the function, at the end of the function, or both,
  600.        according to the respective option.
  601.  
  602.    :PRINT Form
  603.    :PRINT-AFTER Form
  604.    :PRINT-ALL Form
  605.        In addition to the usual prinout, he result of evaluating Form is
  606.        printed at the start of the function, at the end of the function, or
  607.        both, according to the respective option.  Multiple print options cause
  608.        multiple values to be printed.
  609.  
  610.    :FUNCTION Function-Form
  611.        This is a not really an option, but rather another way of specifying
  612.        what function to trace.  The Function-Form is evaluated immediately,
  613.        and the resulting function is traced.
  614.  
  615.    :ENCAPSULATE {:DEFAULT | T | NIL}
  616.        If T, the tracing is done via encapsulation (redefining the function
  617.        name) rather than by modifying the function.  :DEFAULT is the default,
  618.        and means to use encapsulation for interpreted functions and funcallable
  619.        instances, breakpoints otherwise.  When encapsulation is used, forms are
  620.        *not* evaluated in the function's lexical environment, but DEBUG:ARG can
  621.        still be used.
  622.  
  623.    :CONDITION, :BREAK and :PRINT forms are evaluated in the lexical environment
  624.    of the called function; DEBUG:VAR and DEBUG:ARG can be used.  The -AFTER and
  625.    -ALL forms are evaluated in the null environment."
  626.   (if specs
  627.       (expand-trace specs)
  628.       '(%list-traced-functions)))
  629.  
  630.  
  631. ;;;; Untracing:
  632.  
  633. ;;; UNTRACE-1  --  Internal
  634. ;;;
  635. ;;;    Untrace one function.
  636. ;;;
  637. (defun untrace-1 (function-or-name)
  638.   (let* ((fun (trace-fdefinition function-or-name))
  639.      (info (gethash fun *traced-functions*)))
  640.     (cond
  641.      ((not info) (warn "Function is not TRACE'd -- ~S." function-or-name))
  642.      (t
  643.       (cond
  644.        ((trace-info-encapsulated info)
  645.     (ext:unencapsulate (trace-info-what info) 'trace))
  646.        (t
  647.     (di:delete-breakpoint (trace-info-start-breakpoint info))
  648.     (di:delete-breakpoint (trace-info-end-breakpoint info))))
  649.       (setf (trace-info-untraced info) t)
  650.       (remhash fun *traced-functions*)))))
  651.  
  652. ;;; UNTRACE-ALL  --  Internal
  653. ;;;
  654. ;;;    Untrace all traced functions.
  655. ;;;
  656. (defun untrace-all ()
  657.   (dolist (fun (%list-traced-functions))
  658.     (untrace-1 fun))
  659.   t)
  660.  
  661. (defmacro untrace (&rest specs)
  662.   "Removes tracing from the specified functions.  With no args, untraces all
  663.    functions."
  664.   (if specs
  665.       (collect ((res))
  666.     (let ((current specs))
  667.       (loop
  668.         (unless current (return))
  669.         (let ((name (pop current)))
  670.           (res (if (eq name :function)
  671.                `(untrace-1 ,(pop current))
  672.                `(untrace-1 ',name)))))
  673.       `(progn ,@(res) t)))
  674.       '(untrace-all)))
  675.